using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class SysHolidayService : BaseAppService<SysHolidayDto, SysHoliday, SysHolidayRepository>
    {
        readonly ExcelHelp _excelHelp;
        public SysHolidayService(SysHolidayRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        #region 基础
        private LoadPageOffsetModelAuto<SysHoliday, SysHolidayDto>
           GetLoadPageOffsetMode(PagingModel<SysHolidayDto>
               pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("Year", "=") //年份
     .Add("Holiday", "=") //节假日
     .Add("SubFactory", "=");//隶属工厂


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new SysHoliday()
            {
                Id = model.Id,
                Year = model.Year,//年份
                Holiday = model.Holiday,//节假日
                SubFactory = model.SubFactory,//隶属工厂
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<SysHolidayDto>> GetList(PagingModel<SysHolidayDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<SysHolidayDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.Year,
                Year_title = "年份",
                model.Holiday,
                Holiday_title = "节假日",
                model.SubFactory,
                SubFactory_title = "隶属工厂",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

        #endregion


        public bool IsHoliday(DateTime dateTime)
        {
            var day = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
            var dates = _repository.QueryBase().FirstOrDefault(a => a.Holiday == dateTime);
            return dates != null;
        }


    }

}
